package mdm.led.controller;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.LocalConfig;
import mdm.led.entity.excel.ExcelFive;
import mdm.led.entity.excel.ExcelFour;
import mdm.led.entity.excel.ExcelThree;
import mdm.led.util.ExportExcelUtil;

/**
 *
 * @author :Monarch丿He
 * @date :2018年11月5日
 * @explain : 导出excel 表格
 */
@Api(description = "导出excel 表格")
@Controller
@RequestMapping("exportExcel")
public class ExportExcelController {

	@Autowired
	protected LocalConfig localConfig;

	private static final Logger LOGGER = LoggerFactory.getLogger(ExportExcelController.class);

	@ApiOperation("导出督导三查Excel")
	@RequestMapping(value = "exportThreeExcel", method = RequestMethod.POST)
	@ResponseBody
	public void exportThreeExcel(@RequestBody List<ExcelThree> list, HttpServletResponse response) {
		LOGGER.info("\n----------------- 导出督导三查Excel ----------------");
		// 存放路径
		String excelPath = localConfig.getOBJECT_PATH();
		String path = "";
		HSSFWorkbook wb = null;
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			map = ExportExcelUtil.exportThreeExcel(list, excelPath);
			path = (String) map.get("path");
			wb = (HSSFWorkbook) map.get("wb");
			if (path != null && path != "") {
				response.setContentType("application/vnd.ms-excel;charset=gbk");
				response.setHeader("content-disposition",
						"attachment;filename=" + URLEncoder.encode(path, "UTF-8"));
				wb.write(response.getOutputStream());
				wb.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ApiOperation("导出督查四必Excel")
	@RequestMapping(value = "exportFourExcel", method = RequestMethod.POST)
	@ResponseBody
	public void exportFourExcel(@RequestBody List<ExcelFour> list, HttpServletResponse response) {
		LOGGER.info("\n----------------- 导出督查四必Excel ----------------");
		// 存放路径
		String excelPath = localConfig.getOBJECT_PATH();
		String path = "";
		HSSFWorkbook wb = null;
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			map = ExportExcelUtil.exportFourExcel(list, excelPath);
			path = (String) map.get("path");
			wb = (HSSFWorkbook) map.get("wb");
			if (path != null && path != "") {
				response.setContentType("application/vnd.ms-excel;charset=gbk");
				response.setHeader("content-disposition",
						"attachment;filename=" + URLEncoder.encode(path, "UTF-8"));
				wb.write(response.getOutputStream());
				wb.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ApiOperation("导出流程管控Excel")
	@RequestMapping(value = "exportFiveExcel", method = RequestMethod.POST)
	@ResponseBody
	public void exportFiveExcel(@RequestBody List<ExcelFive> list, HttpServletResponse response) {
		LOGGER.info("\n----------------- 导出流程管控Excel ----------------");
		// 存放路径
		String excelPath = localConfig.getOBJECT_PATH();
		String path = "";
		HSSFWorkbook wb = null;
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			map = ExportExcelUtil.exportFiveExcel(list, excelPath);
			path = (String) map.get("path");
			wb = (HSSFWorkbook) map.get("wb");
			if (path != null && path != "") {
				
				/*response.setContentType("application/vnd.ms-excel;charset=gbk");
				response.setHeader("content-disposition",
						"attachment;filename=" + URLEncoder.encode(path, "UTF-8"));
				wb.write(response.getOutputStream());
				wb.close();*/
				this.setResponseHeader(response, path);
				OutputStream os = response.getOutputStream();
		        wb.write(os);
		        os.flush();
		        os.close();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
